const Base = require("./base.js");

module.exports = class extends Base {
  /**
   * 首页数据显示
   * @returns {Promise<*>}
   */
  async pageAction() {
    //精品推荐
    let fields = "id as goods_id,goods_name,primary_pic_url,sale_price";
    const BoutiqueRecom = await this.model("goods")
      .field(fields)
      .where({
        promotion_id: 1,
        is_delete: 0,
        is_on_sale: 1,
      })
      .order("sort desc")
      .select();
    //男装推荐
    const MaleRecom = await this.model("goods")
      .field(fields)
      .where({
        promotion_id: 2,
        is_delete: 0,
        is_on_sale: 1,
      })
      .order("sort desc")
      .select();
    //女装推荐
    const FemaleRecom = await this.model("goods")
      .field(fields)
      .where({
        promotion_id: 3,
        is_delete: 0,
        is_on_sale: 1,
      })
      .order("sort desc")
      .select();
    const list = await this.model("link")
      .where({ linktype: 0, visible: 1 })
      .order("linkorder")
      .select();
    const link = await this.model("link")
      .where({ linktype: 1, visible: 1 })
      .order("linkorder")
      .select();
    return this.success({
      BoutiqueRecom: BoutiqueRecom,
      MaleRecom: MaleRecom,
      FemaleRecom: FemaleRecom,
      list: list,
      link: link,
    });
  }

  /**
   * 商品列表
   * @returns {Promise<*>}
   */
  async listAction() {
    const d = this.post();
    let fields = "id as goods_id,goods_name,primary_pic_url,sale_price";
    if (d.category_id === 0) {
      const GoodsList = await this.model("goods")
        .field(fields)
        .where({
          is_delete: 0,
          is_on_sale: 1,
        })
        .order("add_time desc")
        .page(d.page || 1, d.limit || 20)
        .countSelect();
      return this.success(GoodsList);
    }
    const GoodsList = await this.model("goods")
      .field(fields)
      .where({
        category_id: d.category_id,
        is_delete: 0,
        is_on_sale: 1,
      })
      .order("sort,add_time desc")
      .page(d.page || 1, d.limit || 20)
      .countSelect();
    return this.success(GoodsList);
  }

  /**
   * 商品按名称搜索
   * @returns {Promise<void>}
   */
  async searchAction() {
    let d = this.post();
    let fields = "id as goods_id,goods_name,primary_pic_url,sale_price";
    const GoodsList = await this.model("goods")
      .field(fields)
      .where({
        is_delete: 0,
        is_on_sale: 1,
      })
      .where({ goods_name: ["like", `%${d.sw}%`] })
      .order("sort,add_time desc")
      .page(d.page || 1, d.limit || 20)
      .countSelect();
    return this.success(GoodsList);
  }

  /**
   * 商品信息详情
   * @returns {Promise<*>}
   */
  async detailsAction() {
    const goods_id = this.post("goods_id");
    let info = await this.model("goods").where({ id: goods_id }).find();
    if (think.isEmpty(info)) {
      return this.fail("此商品已被下架");
    }
    // if (info.category_id === 1036008) {
    //     info.style = 'A';
    // }
    // if (info.category_id === 1036009) {
    //     info.style = 'B';
    // }
    // if (info.category_id === 1036010) {
    //     info.style = 'C';
    // }
    //收藏状态
    let collect_status = true;
    const collect = await this.model("collect")
      .where({
        user_id: think.user.user_id,
        goods_id: goods_id,
        status: 0,
      })
      .find();
    if (think.isEmpty(collect)) {
      collect_status = false;
    }
    return this.success({ info: info, collect_status: collect_status });
  }

  /**
   * 商品收藏信息
   * @returns {Promise<void>}
   */
  async collectAction() {
    const d = this.post();
    if (d.goods_id) {
      const collect = await this.model("collect")
        .where({
          user_id: think.user.user_id,
          status: 0,
          goods_id: d.goods_id,
        })
        .getField("goods_id");
      if (collect.length === 0) {
        return this.success(collect);
      }
      const goods = await this.model("goods")
        .where({ id: ["IN", collect] })
        .select();
      return this.success(goods);
    } else {
      const collect = await this.model("collect")
        .where({
          user_id: think.user.user_id,
          status: 0,
        })
        .getField("goods_id");
      if (collect.length === 0) {
        return this.success(collect);
      }
      const goods = await this.model("goods")
        .where({ id: ["IN", collect] })
        .select();
      return this.success(goods);
    }
  }

  /**
   * 收藏状态变更
   * @returns {Promise<*>}
   */
  async collectChangeAction() {
    const d = this.post();
    const collect = await this.model("collect")
      .where({
        user_id: think.user.user_id,
        goods_id: d.goods_id,
      })
      .find();
    if (think.isEmpty(collect)) {
      const add = await this.model("collect").add({
        user_id: think.user.user_id,
        goods_id: d.goods_id,
      });
      return this.success({ status: 0 });
    } else {
      let status = collect.status === 1 ? 0 : 1;
      const add = await this.model("collect")
        .where({
          user_id: think.user.user_id,
          goods_id: d.goods_id,
        })
        .update({ status: status });

      return this.success({ status: status });
    }
  }

  /**
   * 商品分类
   * @returns {Promise<*>}
   */
  async categoryAction() {
    const data = await this.model("category")
      .field('id as category_id,name as category_name,desc,"T" as active')
      .where({
        show: 1,
        status: 0,
        pid: 0,
      })
      .order("sort")
      .select();
    return this.success(data);
  }

  async categoryByIdAction() {
    const man = await this.model("category")
      .field("id as category_id,name as category_name,picurl")
      .where({
        show: 1,
        status: 0,
        pid: 1,
      })
      .order("sort")
      .select();
    man.push({
      category_id: 0,
      category_name: "更多系列",
      picurl: "../../static/img/more.png",
    });
    const woman = await this.model("category")
      .field("id as category_id,name as category_name,picurl")
      .where({
        show: 1,
        status: 0,
        pid: 4,
      })
      .order("sort")
      .select();
    woman.push({
      category_id: 0,
      category_name: "更多系列",
      picurl: "../../static/img/more.png",
    });
    return this.success({ man: man, woman: woman });
  }

  /**
   *
   * 商品浏览记录
   * @returns {Promise<*>}
   */
  async footprintAction() {
    const goods_id = this.post("goods_id") || 0;
    await this.model("footprint").add({
      user_id: think.user.user_id,
      goods_id: goods_id,
    });
    return this.success("记录成功");
  }
};
